package com.lsy.weizhi2.picture;

import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.fragment.MyAblum_frag_dirs;
import com.lsy.weizhi2.fragment.MyAblum_frag_list;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.myutils.AblumCall;


/**
 * 相册Activity
 */

public class MyAblum extends MyBaseActivity implements AblumCall {
    private String Tag = "MyAblum";
    private Context context;
    private MyTitleView my_ablum_title;
    private FrameLayout myablum_fl;
    public static MyAblum_frag_dirs frag_dir;
    public static MyAblum_frag_list frag_list;
    public static android.app.FragmentManager manager;
    public static FragmentTransaction ft;
    public static int flag = 0;
    private String name;
    private int count, xx, height;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_ablum);
        Init();
        context = MyAblum.this;
        myablum_fl = (FrameLayout) findViewById(R.id.myablum_fl);
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = my_ablum_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(my_ablum_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                my_ablum_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
    }

    private void Init() {
        context = MyAblum.this;
        my_ablum_title = (MyTitleView) findViewById(R.id.my_ablum_title);
        myablum_fl = (FrameLayout) findViewById(R.id.myablum_fl);
        frag_dir = new MyAblum_frag_dirs();
        manager = getFragmentManager();
        ft = manager.beginTransaction();
        ft.add(R.id.myablum_fl, frag_dir);
        ft.commit();
    }


    @Override
    public void Select(String name, int count) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putInt("count", count);
        frag_list = new MyAblum_frag_list();
        frag_list.setArguments(bundle);
        ft = manager.beginTransaction();
        ft.replace(R.id.myablum_fl, frag_list);
        flag = 1;
        ft.commit();
    }

    @Override
    public void ImagePath(String path) {
        //返回指定界面
        Intent intent = new Intent();
        intent.putExtra("image", path);
        setResult(1, intent);
        finish();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && flag == 1) {
            ft = manager.beginTransaction();
            ft.replace(R.id.myablum_fl, frag_dir);
            flag = 0;
            ft.commit();
            return true;
        } else {
            finish();
            return true;
        }
    }
}
